<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Activity;
use App\Shop;
use Input;
use DB;
class ActivityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取所有的内容
        $datas = DB::table('activity')
            ->select('activity.actid','activity.actname','activity.adddate','shops.shopname')
            ->join('shops','activity.shopid','=','shops.id')
            ->paginate('30');
        return view('admin.activity.index',compact('datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //根据id查询出所有内容
        $datas = DB::table('activity')
            ->select('activity.*','shops.shopname')
            ->join('shops','activity.shopid','=','shops.id')
            ->first();
        return view('admin.activity.show',compact('datas'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //根据id获取内容
        $datas = DB::table('activity')
            ->select('activity.*','shops.shopname')
            ->join('shops','activity.shopid','=','shops.id')
            ->where('activity.actid','=',$id)
            ->first();
        return view('admin.activity.edit',compact('datas'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //更新数据
        $actname   = trim(Input::get('actname'));
        $acttitle  = trim(Input::get('acttitle'));
        $actimg    = trim(Input::get('actimg'));
        $actdesc   = trim(Input::get('actdesc'));
        $shopid    = trim(Input::get('shopid'));
        $res       = Activity::where('activity.actid','=',$id)
                   ->update(['shopid'=>$shopid,'actname'=>$actname,'acttitle'=>$acttitle,'actimg'=>$actimg,'actdesc'=>$actdesc]);
        if($res)
        return back() -> with(['info'=>'更新成功']);
        return back() -> with(['info'=>'更新失败']);

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除数据
        $res = Activity::where('actid','=',$id)->delete();
        if($res)
        return Response() -> json(['msg'=>'删除成功','status'=>'成功']);
    }
}
